package com.liuxinlong.modules.patent.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Patent;
import com.liuxinlong.modules.patent.controller.request.ImportPatentReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentNoticeReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentOperateHistoryReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentReq;
import com.liuxinlong.modules.patent.service.PatentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专利首页控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-09-06
 */
@RestController
@RequestMapping("/patent")
public class PatentController {

    @Autowired
    private PatentService patentService;

    @Autowired
    private UploadService uploadService;

    /**
     * 查询专利信息
     *
     * @return 专利信息
     */
    @PostMapping("spread/page")
    public BaseResp queryPatentSpreadInfo() {
        Map<String, Object> resultMap = patentService.queryPatentSpreadInfo();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询专利信息成功");
        resp.setDataMap(resultMap);
        return resp;
    }

    /**
     * 查询专利公告信息列表
     *
     * @param req 请求信息
     * @return 专利公告信息列表
     */
    @PostMapping("spread/notice")
    public BaseResp queryPatentNoticeList(@RequestBody QueryPatentNoticeReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("noticeType", req.getNoticeType());
        List<Map<String, Object>> resultList = patentService.queryPatentNoticeList(queryParam);
        int totalNum = patentService.getPatentNoticeCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询专利公告信息列表成功");
        resp.setDataList(resultList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 分页查询专利信息列表
     *
     * @param req 请求信息
     * @return 专利信息
     */
    @PostMapping("page")
    public BaseResp pagePatentInfo(@RequestBody QueryPatentReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("patentSn", req.getPatentSn());
        queryParam.put("name", req.getName());
        queryParam.put("patentee", req.getPatentee());
        queryParam.put("department", req.getDepartment());
        queryParam.put("applyStartDate", req.getApplyStartDate());
        queryParam.put("applyEndDate", req.getApplyEndDate());
        queryParam.put("announcementStartDate", req.getAnnouncementStartDate());
        queryParam.put("announcementEndDate", req.getAnnouncementEndDate());
        queryParam.put("classType", req.getClassType());
        queryParam.put("status", req.getStatus());
        queryParam.put("applyCountry", req.getApplyCountry());
        queryParam.put("inventor", req.getInventor());
        queryParam.put("level", req.getLevel());
        queryParam.put("implementProduct", req.getImplementProduct());
        queryParam.put("highValue", req.getHighValue());
        Map<String, Object> dataMap = patentService.pagePatentInfo(queryParam);
//        int totalNum = patentService.getPatentCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询专利信息列表成功");
        resp.setDataMap(dataMap);
//        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增专利信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增专利", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addPatent(@RequestBody UpdatePatentReq req) {
        Patent patent = new Patent();
        patent.setSn(req.getSn());
        patent.setName(req.getName());
        patent.setBrief(req.getBrief());
        patent.setCompanySn(req.getCompanySn());
        patent.setCaseSn(req.getCaseSn());
        patent.setDepartment(req.getDepartment());
        patent.setPatentee(req.getPatentee());
        patent.setPatenteeFirst(req.getPatenteeFirst());
        patent.setInventor(req.getInventor());
        patent.setPatentSn(req.getPatentSn());
        patent.setClassType(req.getClassType());
        patent.setLevel(req.getLevel());
        patent.setStatus(req.getStatus());
        patent.setStatusDesc(req.getStatusDesc());
        patent.setApplyDate(req.getApplyDate());
        patent.setAnnouncementDate(req.getAnnouncementDate());
        patent.setRemark(req.getRemark());
        patent.setAccessType(req.getAccessType());
        patent.setImplementProduct(req.getImplementProduct());
        patent.setImplementDate(req.getImplementDate());
        patent.setRelationProject(req.getRelationProject());
        patent.setPatentEngineer(req.getPatentEngineer());
        patent.setHighValue(req.getHighValue());
        patentService.addPatent(patent);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增专利信息成功");
        return resp;

    }

    /**
     * 修改专利信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改专利", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePatent(@RequestBody UpdatePatentReq req) {
        Patent patent = new Patent();
        patent.setId(req.getId());
        patent.setSn(req.getSn());
        patent.setName(req.getName());
        patent.setBrief(req.getBrief());
        patent.setCompanySn(req.getCompanySn());
        patent.setCaseSn(req.getCaseSn());
        patent.setDepartment(req.getDepartment());
        patent.setPatentee(req.getPatentee());
        patent.setPatenteeFirst(req.getPatenteeFirst());
        patent.setInventor(req.getInventor());
        patent.setPatentSn(req.getPatentSn());
        patent.setClassType(req.getClassType());
        patent.setLevel(req.getLevel());
        patent.setStatus(req.getStatus());
        patent.setStatusDesc(req.getStatusDesc());
        patent.setApplyDate(req.getApplyDate());
        patent.setAnnouncementDate(req.getAnnouncementDate());
        patent.setRemark(req.getRemark());
        patent.setAccessType(req.getAccessType());
        patent.setImplementProduct(req.getImplementProduct());
        patent.setImplementDate(req.getImplementDate());
        patent.setRelationProject(req.getRelationProject());
        patent.setPatentEngineer(req.getPatentEngineer());
        patent.setHighValue(req.getHighValue());
        patentService.updatePatent(patent,req.getPushFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专利信息成功");
        return resp;
    }

    /**
     * 删除专利信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除专利", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deletePatent(@RequestBody BaseDeleteReq req) {
        patentService.deletePatent(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专利信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入专利数据", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importPatents(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = patentService.importPatent((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入专利信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入专利部门数据", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddPatent(@RequestBody ImportPatentReq req) {
        return patentService.batchAddPatent(req.getDataList());
    }

    /**
     * 导出专利信息
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出专利信息", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportPatent(@RequestBody QueryPatentReq req, HttpServletResponse response) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("patentSn", req.getPatentSn());
        queryParam.put("name", req.getName());
        queryParam.put("patentee", req.getPatentee());
        queryParam.put("department", req.getDepartment());
        queryParam.put("applyStartDate", req.getApplyStartDate());
        queryParam.put("applyEndDate", req.getApplyEndDate());
        queryParam.put("announcementStartDate", req.getAnnouncementStartDate());
        queryParam.put("announcementEndDate", req.getAnnouncementEndDate());
        queryParam.put("classType", req.getClassType());
        queryParam.put("status", req.getStatus());
        queryParam.put("applyCountry", req.getApplyCountry());
        queryParam.put("inventor", req.getInventor());
        queryParam.put("level", req.getLevel());
        queryParam.put("implementProduct", req.getImplementProduct());
        queryParam.put("highValue", req.getHighValue());
        patentService.exportPatent(queryParam, response);
    }

    /**
     * 分页查询专利操作信息列表
     *
     * @param req 请求信息
     * @return 专利检索信息列表
     */
    @PostMapping("history/page")
    public BaseResp pagePatentOperateHistory(@RequestBody QueryPatentOperateHistoryReq req) {
        Map<String, Object> queryPram = new HashMap<>();
        queryPram.put("startNum", req.getStartNum());
        queryPram.put("pageSize", req.getPageSize());
        queryPram.put("periodType", req.getPeriodType());
        queryPram.put("operateType", req.getOperateType());
        Map<String, Object> dataMap = patentService.pagePatentOperateHistory(queryPram);
//        int totalNum = patentService.getPatentOperateHistoryCount(queryPram);
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
//        resp.setTotalNum(totalNum);
        resp.setRetMessage("分页查询专利操作信息列表成功");
        return resp;
    }

    /**
     * 删除专利操作历史记录
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("history/delete")
    @LogAnnotation(value = "删除专利操作历史记录", business = LogBusinessConstants.PATENT_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deletePatentOperateHistory(@RequestBody BaseDeleteReq req) {
        patentService.deletePatentOperateHistory(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专利操作历史记录成功");
        return resp;
    }

    /**
     * 查询专利拓展信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("expand")
    public BaseResp queryPatentExpand(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = patentService.queryPatentExpand(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询专利拓展信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }
}
